package docker

//自定义网络 -- 容器互联
//docker network 常用命令
//docker0问题，它不支持容器名的链接访问，切一部分功能受限，所以需要只定义网络
//准备  停止并删除所有容器
//   docker stop $(docker ps -aq)
//   docker rm $(docker ps -aq)

//docker自定义网络已经维护好了对应的关系，所以推荐平时使用网络
//docker自定义网络的好处，不同的集群使用不同的网络，保证集群是健康的
//redis 集群  搭建一个redis网络  192.160.0.0
//mysql 集群 搭建一个mysql网络   192.161.0.0  //是相互隔离的，当然，相互之间也是可以打通的，看 docker-43-网络联通
//docker net
//	1.容器间的互联和通信以及端口映射
//  2.容器ip变动的时候可以通过服务名直接网络通信而不受到ip的变化影响
/*

docker network ls
	网络模式
	bridge : 桥接 docker (默认，自己创建也使用bridge模式)
	none : 不配置网络
	host : 和宿主机共享网络
	container : 容器内网络连通（用得少，局限大）


目录：
	1.net所有命令
	2.ls 查看所有网络
	3.create 创建网络
		3.1 使用自定义网络后，可直接根据[容器名]ping通交互,不需要ip地址，如此一来，实现高可用，某台服务器坏掉了，ip不需要再改ip

	4.inspect [network id] 查看某一网络信息 配合 inspect [容器id]
		docker network inspect mynet
		docker network inspect mynet | grep name
		ifconfig | grep docker

--------------net所有命令--------------
	docker network --help

	Commands:
		#链接 一个容器到一个网络
	  connect     Connect a container to a network
		#创建
	  create      Create a network
		#中断
	  disconnect  Disconnect a container from a network
		#查看信息
	  inspect     Display detailed information on one or more networks
		#查看所哟列表
	  ls          List networks
		#删除  删除所有无效网络，和虚悬镜像一样
	  prune       Remove all unused networks
		#删除一个或多个网络
	  rm          Remove one or more networks





--------------ls 查看所有网络--------------

	docker network ls

	# netId       网络模式
	NETWORK ID     NAME      DRIVER    SCOPE
	#这是docker0
	fd299780b2c3   bridge    bridge    local
	4225d9ee0f15   host      host      local
	15672c797008   none      null      local


	网络模式
	bridge : 桥接 docker (默认，自己创建也使用bridge模式)
	none : 不配置网络
	host : 和宿主机共享网络
	container : 容器内网络连通（用得少，局限大）



--------------create 创建网络--------------

	之前启动容器的命令   docker run -it -d -P --name mysql01 mysql
		实际上默认添加绑定 --net bridge  而  bridge 就是docker0
			docker run -it -d -P --name mysql01  --net bridge  mysql

	docker0 的特点
		1.默认
		2.域名不可访问，--link是可以打通

	自定义网路

		docker network create --help


Usage:  docker network create [OPTIONS] NETWORK

Create a network

Options:
      --attachable           Enable manual container attachment
      --aux-address map      Auxiliary IPv4 or IPv6 addresses used by Network driver (default map[])
      --config-from string   The network from which to copy the configuration
      --config-only          Create a configuration only network
  -d, --driver string        Driver to manage the Network (default "bridge")
      --gateway strings      IPv4 or IPv6 Gateway for the master subnet
      --ingress              Create swarm routing-mesh network
      --internal             Restrict external access to the network
      --ip-range strings     Allocate container ip from a sub-range
      --ipam-driver string   IP Address Management Driver (default "default")
      --ipam-opt map         Set IPAM driver specific options (default map[])
      --ipv6                 Enable IPv6 networking
      --label list           Set metadata on a network
  -o, --opt map              Set driver specific options (default map[])
      --scope string         Control the network's scope
      --subnet strings       Subnet in CIDR format that represents a network segment



	//创建一个自定义网络
	docker network create --driver bridge --subnet 192.168.0.0/16 --gateway 192.168.0.1 mynet
		--driver bridge 使用桥接模式
		--subnet 192.168.0.1/16 子网 其中/16 表示
				192		 168		0		 1
			00000000   00000000  00000000  00000000
			每一个都是8位，/16表示 256*256 -2 = 65535个网络 = (00000000   00000000) * (00000000  00000000) z种子码
						 /24  = 255 种
			范围 198.168.0.2 - 198.168.255.255 ,因为0.0 和0.1 上面创建时已经使用了

		--gateway 192.168.0.1  网管，从哪里出去


#创建网络
root@VM-4-8-debian:~# docker network create --driver bridge --subnet 192.168.0.0/16 --gateway 192.168.0.1 mynet
6f6d0f570d9094e25ee160aff963485037e0fd793a66f267cdc246eeb189dec6
root@VM-4-8-debian:~# docker network ls
NETWORK ID     NAME      DRIVER    SCOPE
fd299780b2c3   bridge    bridge    local
4225d9ee0f15   host      host      local
6f6d0f570d90   mynet     bridge    local	#发现已经创建的网络
15672c797008   none      null      local
root@VM-4-8-debian:~# docker network inspect 6f6d0f570d90
[
    {
        "Name": "mynet",
        "Id": "6f6d0f570d9094e25ee160aff963485037e0fd793a66f267cdc246eeb189dec6",
        "Created": "2022-09-29T18:21:30.973154311+08:00",
        "Scope": "local",
        "Driver": "bridge",
        "EnableIPv6": false,
        "IPAM": {
            "Driver": "default",
            "Options": {},
            "Config": [
                {
                    "Subnet": "192.168.0.0/16",		#已经创建的网络
                    "Gateway": "192.168.0.1"		#网关
                }
            ]
        },
        "Internal": false,
        "Attachable": false,
        "Ingress": false,
        "ConfigFrom": {
            "Network": ""
        },
        "ConfigOnly": false,
        "Containers": {},
        "Options": {},
        "Labels": {}
    }
]
root@VM-4-8-debian:~#

	使用自定义网络启动容器

root@VM-4-8-debian:~# docker run -it -d --name mysql-mynet-01 --net mynet mysql /bin/bash
76ab8685b4f59d76dda62cf755a320e9b69d459f0516d09a910f4e8184adc4e6
root@VM-4-8-debian:~# docker ps
CONTAINER ID   IMAGE     COMMAND                  CREATED         STATUS         PORTS                 NAMES
76ab8685b4f5   mysql     "docker-entrypoint.s…"   7 seconds ago   Up 6 seconds   3306/tcp, 33060/tcp   mysql-mynet-01
root@VM-4-8-debian:~#
root@VM-4-8-debian:~# docker inspect 76ab8685b4f5
[
    {
        "Id": "76ab8685b4f59d76dda62cf755a320e9b69d459f0516d09a910f4e8184adc4e6",
		.....
        "Mounts": [
            {
                "Type": "volume",
                "Name": "9a230e89348ede57bdc6895cf61059495f9b81f6dfa31a985da43e867e400c1d",
                "Source": "/var/lib/docker/volumes/9a230e89348ede57bdc6895cf61059495f9b81f6dfa31a985da43e867e400c1d/_data",
                "Destination": "/var/lib/mysql",
                "Driver": "local",
                "Mode": "",
                "RW": true,
                "Propagation": ""
            }
        ],
     	..........
        "NetworkSettings": {
            "Bridge": "",
            "SandboxID": "5a4a8cfff502d731bffacfcf5694eae2fe76f919f15b15b8a9f1ce206282fa64",
            "HairpinMode": false,
            "LinkLocalIPv6Address": "",
            "LinkLocalIPv6PrefixLen": 0,
            "Ports": {
                "3306/tcp": null,
                "33060/tcp": null
            },
            "SandboxKey": "/var/run/docker/netns/5a4a8cfff502",
            "SecondaryIPAddresses": null,
            "SecondaryIPv6Addresses": null,
            "EndpointID": "",
            "Gateway": "",
            "GlobalIPv6Address": "",
            "GlobalIPv6PrefixLen": 0,
            "IPAddress": "",
            "IPPrefixLen": 0,
            "IPv6Gateway": "",
            "MacAddress": "",
            "Networks": {
                "mynet": {
                    "IPAMConfig": null,
                    "Links": null,
                    "Aliases": [
                        "76ab8685b4f5"
                    ],
                    "NetworkID": "6f6d0f570d9094e25ee160aff963485037e0fd793a66f267cdc246eeb189dec6",
                    "EndpointID": "2b7b0a9fd3e99e38ebfe40e243c5e0ff73eeb8f96bf795828b39ebb84c3f56b1",
                    "Gateway": "192.168.0.1",    ######## 网关
                    "IPAddress": "192.168.0.2",	 ######## 分配了ip
                    "IPPrefixLen": 16,
                    "IPv6Gateway": "",
                    "GlobalIPv6Address": "",
                    "GlobalIPv6PrefixLen": 0,
                    "MacAddress": "02:42:c0:a8:00:02",
                    "DriverOpts": null
                }
            }
        }
    }
]
root@VM-4-8-debian:~#
root@VM-4-8-debian:~# docker run -it -d --name mysql-mynet-02 --net mynet mysql /bin/bash
f794ece27a8735dcfed1228bdda509e278686758f327350abb11107f8cae5ab3
root@VM-4-8-debian:~# docker network inspect mynet         ####查看配置
[
    {
        "Name": "mynet",
        "Id": "6f6d0f570d9094e25ee160aff963485037e0fd793a66f267cdc246eeb189dec6",
        "Created": "2022-09-29T18:21:30.973154311+08:00",
        "Scope": "local",
        "Driver": "bridge",
        "EnableIPv6": false,
        "IPAM": {
            "Driver": "default",
            "Options": {},
            "Config": [
                {
                    "Subnet": "192.168.0.0/16",
                    "Gateway": "192.168.0.1"
                }
            ]
        },
        "Internal": false,
        "Attachable": false,
        "Ingress": false,
        "ConfigFrom": {
            "Network": ""
        },
        "ConfigOnly": false,
        "Containers": {
            "76ab8685b4f59d76dda62cf755a320e9b69d459f0516d09a910f4e8184adc4e6": {
                "Name": "mysql-mynet-01",					###容器1
                "EndpointID": "2b7b0a9fd3e99e38ebfe40e243c5e0ff73eeb8f96bf795828b39ebb84c3f56b1",
                "MacAddress": "02:42:c0:a8:00:02",
                "IPv4Address": "192.168.0.2/16",			###分配的ip
                "IPv6Address": ""
            },
            "f794ece27a8735dcfed1228bdda509e278686758f327350abb11107f8cae5ab3": {
                "Name": "mysql-mynet-02", 						###容器2
                "EndpointID": "5e141acfb8e4cf3b9cc064acb992bac90c027746c418eef8bf22f3798a532795",
                "MacAddress": "02:42:c0:a8:00:03",
                "IPv4Address": "192.168.0.3/16",			###分配的ip
                "IPv6Address": ""
            }
        },
        "Options": {},
        "Labels": {}
    }
]
root@VM-4-8-debian:~#



		------------使用自定义网络后，可直接根据容器名ping通交互------------

		root@VM-4-8-debian:~# docker exec -it mysql-mynet-01 ping 192.168.0.3
		root@VM-4-8-debian:~# docker exec -it mysql-mynet-01 ping mysql-mynet-02




--------------inspect [network id] 查看某一网络信息 配合 inspect [容器id]--------------

	docker network inspect  [network Id]
	配合
	docker inspect [容器id]

	docker network inspect fd299780b2c3
	docker network inspect fd299780b2c3 | grep name

root@VM-4-8-debian:~# docker network inspect fd299780b2c3
[
    {
		## 这是 docker0 的配置
        "Name": "bridge",
        "Id": "fd299780b2c3630f409f98392eb2d3ea6513d1e82e2be892ddf0613704736a2c",
        "Created": "2022-09-28T16:06:46.25662191+08:00",
        "Scope": "local",
        "Driver": "bridge",
        "EnableIPv6": false,
        "IPAM": {
            "Driver": "default",
            "Options": null,
            "Config": [
                {
                    "Subnet": "172.17.0.0/16",	# /16   可分配  255.255.(0-255).(0-255) = 256*256-2 = 25535 gg个地址
                    "Gateway": "172.17.0.1"	# 网关 ，这就是 docker0
                }
            ]
        },
        "Internal": false,
        "Attachable": false,
        "Ingress": false,
        "ConfigFrom": {
            "Network": ""
        },
        "ConfigOnly": false,
        "Containers": {					##容器
            "56ff359a6b2dc61eec4a60b51f053014b26222c8b1ebc91da9298ab8f4ad1dcb": {
                "Name": "stupefied_swartz",	##容器名
                "EndpointID": "c72270f4fd6244c4d20da8bda4667d5001790bec0159c8710c7079cb24a152c2",
                "MacAddress": "02:42:ac:11:00:03",
                "IPv4Address": "172.17.0.3/16",			##容器分配ip ，docker分配
                "IPv6Address": ""
            },
            "c81772b242cf9def42a482a39385dc6d887468a7428f9e89e611630ddfd2c1f5": {
                "Name": "frosty_banach",
                "EndpointID": "b86064a56aed0ad8ce449ed671232104cbf2837c2f08dd1824626fe1aba4824e",
                "MacAddress": "02:42:ac:11:00:02",
                "IPv4Address": "172.17.0.2/16",
                "IPv6Address": ""
            }
        },
        "Options": {
            "com.docker.network.bridge.default_bridge": "true",
            "com.docker.network.bridge.enable_icc": "true",
            "com.docker.network.bridge.enable_ip_masquerade": "true",
            "com.docker.network.bridge.host_binding_ipv4": "0.0.0.0",
            "com.docker.network.bridge.name": "docker0",
            "com.docker.network.driver.mtu": "1500"
        },
        "Labels": {}
    }
]
root@VM-4-8-debian:~#









*/
